﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace Turtle8
{
    public enum Key { SpaceBar = 32, Hyphen = 45, BackSpace = 8, Apostrophe = 39 };
    public enum Mask {None, Name, CellPhone, HomePhone, ArmyId, SocialId, Num, Street, RelativeAdress, Email};
    public class MaskedTextBox : System.Windows.Forms.TextBox
    {
        private Mask m_mask = Mask.None;

        public Mask Masked
        {
            get { return m_mask; }
            set
            {
                m_mask = value;
                this.Text = string.Empty;
            }
        }

        public System.Windows.Forms.ErrorProvider errorProvider1;
        private IContainer components;

        public MaskedTextBox()
        {
            InitializeComponent();
        }

        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.errorProvider1 = new System.Windows.Forms.ErrorProvider(this.components);
            ((System.ComponentModel.ISupportInitialize)(this.errorProvider1)).BeginInit();
            this.SuspendLayout();
            // 
            // errorProvider1
            // 
            this.errorProvider1.BlinkStyle = System.Windows.Forms.ErrorBlinkStyle.NeverBlink;
            // 
            // MaskedTextBox
            // 
            this.Leave += new System.EventHandler(this.MaskedTextBox_Leave);
            this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.MaskedTextBox_KeyPress);
            ((System.ComponentModel.ISupportInitialize)(this.errorProvider1)).EndInit();
            this.ResumeLayout(false);

        }
        private void MaskedTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            MaskedTextBox sd = (MaskedTextBox)sender;
            switch (m_mask)
            {
                case Mask.CellPhone:
                case Mask.HomePhone:
                    if (!Char.IsDigit(e.KeyChar) && (e.KeyChar != (int)Key.BackSpace) && (e.KeyChar != (int)Key.Hyphen))
                    {
                        e.Handled = true;
                        errorProvider1.SetError(this, "ניתן להכניס רק ספרות ומקף");
                    }
                    break;
                case Mask.ArmyId:
                case Mask.SocialId:
                case Mask.Num:
                    if (!Char.IsDigit(e.KeyChar) && (e.KeyChar != (int)Key.BackSpace))
                    {
                        e.Handled = true;
                        errorProvider1.SetError(this, "ניתן להכניס רק ספרות");
                    }
                    break;

                case Mask.Name:
                    if (!Char.IsLetter(e.KeyChar) && (e.KeyChar != (int)Key.BackSpace) && (e.KeyChar != (int)Key.Apostrophe) && (e.KeyChar != (int)Key.Hyphen))
                    {
                        e.Handled = true;
                        errorProvider1.SetError(this, "מקש לא פעיל");
                    }
                    break;

                case Mask.Street:
                    if (!Char.IsLetter(e.KeyChar) && (e.KeyChar != (int)Key.BackSpace) && (e.KeyChar != (int)Key.Apostrophe) && (e.KeyChar != (int)Key.SpaceBar))
                    {
                        e.Handled = true;
                        errorProvider1.SetError(this, "מקש לא פעיל");
                    }
                    break;

                case Mask.RelativeAdress:
                    if (!Char.IsLetter(e.KeyChar) && !Char.IsDigit(e.KeyChar) && (e.KeyChar != (int)Key.Apostrophe) && (e.KeyChar != (int)Key.BackSpace) && (e.KeyChar != (int)Key.SpaceBar) && (e.KeyChar != (int)Key.Hyphen))
                    {
                        e.Handled = true;
                        errorProvider1.SetError(this, "מקש לא פעיל");
                    }
                    break;

                case Mask.Email:
                    break;


            }
        }

        private void MaskedTextBox_Leave(object sender, EventArgs e)
        {
            MaskedTextBox sd = (MaskedTextBox)sender;
            Regex regStr;
            Regex regStr2;
            switch (m_mask)
            {
                case Mask.CellPhone:
                    regStr = new Regex(@"05\d{1}-\d{7}");
                    if (((!regStr.IsMatch(sd.Text)) || (sd.Text.Length != 11)) && (sd.Text.CompareTo("") != 0))
                        errorProvider1.SetError(this, "05*-******* מספר פלאפון חייב להיות מהצורה");
                    else
                        errorProvider1.Clear();
                    break;
                
                case Mask.HomePhone:
                    regStr = new Regex(@"0\d{1}-\d{7}");
                    if (((!regStr.IsMatch(sd.Text)) || (sd.Text.Length != 10)) && (sd.Text.CompareTo("") != 0))
                        errorProvider1.SetError(this, "0*-******* מספר טלפון חייב להיות מהצורה");
                    else
                        errorProvider1.Clear();
                    break;

                case Mask.ArmyId:
                    regStr = new Regex(@"\d{7}");
                    if ((!regStr.IsMatch(sd.Text)) || (sd.Text.Length != 7))
                        errorProvider1.SetError(this, "מספר אישי חייב להכיל 7 ספרות בדיוק");
                    else
                        errorProvider1.Clear();
                    break;

                case Mask.SocialId:
                    regStr = new Regex(@"\d{9}");
                    regStr2 = new Regex(@"\d{8}");
                    if (((!regStr.IsMatch(sd.Text)) && (!regStr2.IsMatch(sd.Text)))
                        || (sd.Text.Length < 8) || (sd.Text.Length > 9))
                        errorProvider1.SetError(this, "תעודת זהות חייבת להכיל 8 או 9 ספרות");
                    else
                        errorProvider1.Clear();

                    break;

                case Mask.Num:
                    if (sd.Text.Length > 4)
                        errorProvider1.SetError(this, "מספר ארוך מידי");
                    else
                        errorProvider1.Clear();
                    break;

                case Mask.Email:
                    if ((!sd.Text.Contains("@")) && (sd.Text.CompareTo("") != 0))
                        errorProvider1.SetError(this, "'@' :אימייל חייב להכיל את הסימן");
                    else
                        errorProvider1.Clear();

                    break;

                case Mask.Name:
                case Mask.Street:
                case Mask.RelativeAdress:
                    errorProvider1.Clear();
                    break;
            }
        }
    }
}
